from odps import ODPS
import time
import pandas as pd

def OneShot(current_reserve, bid_price):
    if current_reserve >= bid_price:
        next_reserve = current_reserve * (1 - 0.17121773288551806)
    elif current_reserve > bid_price - 97.7814139986613:
        next_reserve = current_reserve * (1 + 1.882034945196921e-05)
    else:
        next_reserve = current_reserve * (1 + 0.16378698129810268)

    return next_reserve






if __name__ == '__main__':

    df_012 = get_odps_data(table_name = ' ')
    df_34 = get_odps_data(table_name = ' ')
    df_5 = get_odps_data(table_name = ' ')
    df_a = pd.concat([df_012[0:68000], df_34[29000:]], ignore_index=True)
    df_a = df_a.sample(frac=1, random_state=8).reset_index(drop=True)
    df_b = pd.concat([df_34[0:29000], df_5[96000:], df_012[69000:]], ignore_index=True)
    df_b = df_b.sample(frac=1, random_state=12).reset_index(drop=True)
    df_c = pd.concat([df_5[0:96000],df_012[68000:69000]], ignore_index=True)
    df_c = df_c.sample(frac=1, random_state=15).reset_index(drop=True)
    combined_df_list = [df_a[0:25000], df_b[0:10000], df_c[0:35000], df_a[25000:50000], df_b[10000:20000], df_c[35000:70000], df_a[50000:], df_b[20000:], df_c[70000:]]  # 用于存储所有拼接的DataFrame片段
    df = pd.concat(combined_df_list, ignore_index=True)
    df['bidding_price'] = df['bidding_price'].astype(int)
    bidprice_21_list = df['bidding_price'].values.tolist()

    settle = 2
    reserve_price21 = 302.74513745199454
    income = 0
    deal_counter = 0
    max_income = 0
    rationality = 0

    for i in range(len(bidprice_21_list)):
        max_income += bidprice_21_list[i]
        if bidprice_21_list[i] >= reserve_price21:
            deal_counter += 1
            if settle == 1:
                income += bidprice_21_list[i]
            if settle == 2:
                income += reserve_price21
        reserve_price21 = OneShot(reserve_price21, bidprice_21_list[i])
        if abs(bidprice_21_list[i] - reserve_price21) <= 15:
            rationality += 1  

    avg_icome = income / len(bidprice_21_list)
    avg_max_income = max_income / len(bidprice_21_list)
    avg_settle = income / deal_counter
    difference = (max_income - income) / len(bidprice_21_list)
    e = deal_counter / len(bidprice_21_list)
    f = rationality / len(bidprice_21_list)
    # print("average income: ", avg_icome, "max average income: ", avg_max_income, "avg_settle: ", avg_settle, "record num: ", len(bidprice_21_list))
    print("总数据量：", len(bidprice_21_list), end=" ")
    print("总平均出价：", '%.2f'% avg_max_income, end=" ")
    print("总平均收益：", '%.2f'% avg_icome, end=" ")
    print("总差值: ", '%.2f'% difference, end = " ")
    print("总平均成交价: ", '%.2f'% avg_settle, end = " ")
    print("总成交率: ", '%.2f'% e, end = " ")
    print("总合理度："'%.2f'% f)

